import axios from 'axios'
import { Loading, Message } from 'element-ui'
// import response from 'koa/lib/response';
const api_base_url = "https://bird.ioliu.cn"
const request = axios.create({
  // baseUrl: "https://bird.ioliu.cn",
  baseURL:api_base_url,
  timeout:5000
})
let loadingRequest;
request.interceptors.request.use(
  (config) => {
   loadingRequest=Loading.service({
      text:'加载中'
  })
    return config
}, (error) => {
  loadingRequest.close()
    Message.error({ message: '请求超时' })
    return Promise.reject(error)
  }
)
request.interceptors.response.use(response => {
  loadingRequest.close()
  let data = response.data
  let status = response.status
  if (status === 200) {
    return Promise.resolve(data)
  } else {
    Message.error({ message: '服务器好像出现问题了' })
    return Promise.reject(response)
  }
}, error => {
  console.log(error);
  // return Promise.reject(error)
})

// function instance(url, method, data) {
//   // 俩个事儿  1 instance方法执行  2.把执行结果return
//   return request({
//     url,
//     method,
//     [method.toUpperCase() === "GET" ? "params" : "data"]: data,
//   })
// }
export default request
